import {Request} from "../../utils/request";
import host from "../../utils/host";
const HostUrl = host.service;
const Requ = new Request();
var app = getApp()
var goods
Page({
  data: {
    content: [],
    px: [],
    pxopen: false,
    pxshow: false,
    active: true,
    optionList: [],

    movableViewInfo: {
      y: 0,
      showClass: 'none',
      data: {}
    },

    pageInfo: {
      rowHeight: 100,
      scrollHeight: 100,
      startIndex: null,
      scrollY: true,
      readyPlaceIndex: null,
      startY: 0,
      selectedIndex: null,
    }
  },
  onShow: function (e) {
    // console.log(this.data.groupid) // 商品分组id
    if (this.data.groupid == undefined){
      var  data = 1
    }else{
      var data = this.data.groupid
    }
    const promise = new Promise((resolve, reject)=>{
      new Request('get_shopgroup.cls.php').request(
        () => {
          return {
            data: 3,
          }
        },
        (res) => {
          this.setData({
            data: res.data,
            optionListlength: res.data.groups.length,
            groups:res.data.groups
          });
          res.data.groups.length!=0?resolve(res):reject(res);
      });
    });
    promise.then(()=>{
      new Request('get_shopgoodslist.cls.php').request(
        () => {
          return {
            data: data,
            num:0,
          }
        },
        (res) => {
			console.log(res)
          let Response = typeof(res.data['goods']);
          if(Response=="undefined"){
            Response?Requ.showToast("暂无当前商品分类，请选择分类"):"";
            return;
          }
          goods = res.data.goods;
            for(var n=0;n<goods.length; n++){
              if(goods[n].state == 1){
                goods[n].switch1Checked = true
              }else{
                goods[n].switch1Checked = false
              }
              goods[n].image = HostUrl.HostName + goods[n].image;
            }
            this.setData({
              data:res.data,
              groupname: res.data.goods[0].groupname,
              optionList: res.data.goods,
              allgoodsnum: res.data.allgoodsnum
            })
      });
    }, ()=>{
      Requ.showToast("暂无分组列表");
    })

    
  },
  goods_group:function(e){
    setTimeout(function () {
      wx.navigateTo({
        url: '/pages/goods_group/goods_group'
      })
    }, 200)
  },
  goods_build: function (e) {
    setTimeout(function () {
      wx.navigateTo({
        url: '/pages/goods_build/goods_build'
      })
    }, 200)
  },
  listpx: function (e) {
    if (this.data.pxopen) {
      this.setData({
        pxopen: false,
        pxshow: false,
        active: true,
      })
    } else {
      this.setData({
        content: this.data.px,
        pxopen: true,
        pxshow: false,
        active: false,
      })
    }
  },
  choicegroup:function(e){
    if (this.data.pxopen) {
      this.setData({
        groupname: e.currentTarget.dataset.groupname,
        groupid: e.currentTarget.dataset.groupid,
        pxopen: false,
        pxshow: false,
        active: true,
      })
    } else {
      this.setData({
        groupname: e.currentTarget.dataset.groupname,
        groupid: e.currentTarget.dataset.groupid,
        pxopen: true,
        pxshow: false,
        active: false,
      })
    }
    // console.log(this.data.groupid) // 商品分组id
    this.onShow();
  },
  goodsmodify: function (e) {
    setTimeout(function () {
      wx.navigateTo({
        url: '/pages/goodsmodify/goodsmodify?goodsid=' + e.currentTarget.dataset.goodsid
      })
    }, 200)
  },

  //商店列表
  dragStart: function (event) {
    var startIndex = event.target.dataset.index
    console.log(this.data.optionList)
    console.log('获取到的元素为', this.data.optionList[startIndex])
    // 初始化页面数据
    var pageInfo = this.data.pageInfo
    pageInfo.startY = event.touches[0].clientY
    pageInfo.readyPlaceIndex = startIndex
    pageInfo.selectedIndex = startIndex
    pageInfo.scrollY = false
    pageInfo.startIndex = startIndex

    this.setData({
      'movableViewInfo.y': pageInfo.startY - (pageInfo.rowHeight / 2)
    })
    // 初始化拖动控件数据
    var movableViewInfo = this.data.movableViewInfo
    movableViewInfo.data = this.data.optionList[startIndex].name
    movableViewInfo.image = this.data.optionList[startIndex].image
    movableViewInfo.price = this.data.optionList[startIndex].price
    movableViewInfo.groupname = this.data.optionList[startIndex].groupname
    movableViewInfo.showClass = "inline"
    if( this.data.seckill_id>0){
      movableViewInfo.seckill_id = this.data.optionList[startIndex].seckill_id
      movableViewInfo.seckill_price = this.data.optionList[startIndex].seckill_price
    }

    this.setData({
      movableViewInfo: movableViewInfo,
      pageInfo: pageInfo,
      carStatus:true
    })
  },

  dragMove: function (event) {
    var optionList = this.data.optionList
    console.log(optionList)
    var pageInfo = this.data.pageInfo
    // 计算拖拽距离
    var movableViewInfo = this.data.movableViewInfo
    var movedDistance = event.touches[0].clientY - pageInfo.startY
    movableViewInfo.y = pageInfo.startY - (pageInfo.rowHeight / 2) + movedDistance
    // console.log('移动的距离为', movedDistance)

    // 修改预计放置位置
    var movedIndex = parseInt(movedDistance / pageInfo.rowHeight)
    var readyPlaceIndex = pageInfo.startIndex + movedIndex
    if (readyPlaceIndex < 0) {
      readyPlaceIndex = 0
    }else if (readyPlaceIndex >= optionList.length) {
      readyPlaceIndex = optionList.length - 1
    }

    if (readyPlaceIndex != pageInfo.selectedIndex) {
      var selectedData = optionList[pageInfo.selectedIndex]

      optionList.splice(pageInfo.selectedIndex, 1)
      optionList.splice(readyPlaceIndex, 0, selectedData)
      pageInfo.selectedIndex = readyPlaceIndex
    }
    // 移动movableView
    pageInfo.readyPlaceIndex = readyPlaceIndex
    // console.log('移动到了索引', readyPlaceIndex, '选项为', optionList[readyPlaceIndex])

    this.setData({
      movableViewInfo: movableViewInfo,
      optionList: optionList,
      pageInfo: pageInfo,
      dragMove:false
    })
  },

  dragEnd: function (event) {
    // 重置页面数据
    var pageInfo = this.data.pageInfo
    pageInfo.readyPlaceIndex = null
    pageInfo.startY = null
    pageInfo.selectedIndex = null
    pageInfo.startIndex = null
    pageInfo.scrollY = true
    // 隐藏movableView
    var movableViewInfo = this.data.movableViewInfo
    movableViewInfo.showClass = 'none'

    this.setData({
      pageInfo: pageInfo,
      movableViewInfo: movableViewInfo
    })
  },
  //上下架商品
  switchChange:function(e){
    var that = this
    console.log()
    goods[e.currentTarget.dataset.index].switch1Checked = false
    if (e.detail.value == true) {
      this.setData({
        switchChange: 1
      })
    } else {
      this.setData({
        switchChange: 2
      })
    }
    new Request('updata_shopgoods.cls.php').request(
      () => {
        return {
          data:1,
        goodsid: e.currentTarget.dataset.goodsid,
        switchChange: this.data.switchChange
        }
      },
      (res) => {
        that.onShow();
    });
  },
  
  longTap: function (e) {
    wx.showModal({ //使用模态框提示用户进行操作
      title: '提示',
      content: '将删除该商品',
      success: res => {
        if (res.confirm) {
          console.log('用户点击确定')
          var that = this;
          new Request('delete_shopgoods_info.cls.php').request(
            () => {
              return {
                goodsid: e.currentTarget.dataset.groupindex,
              }
            },
            (res) => {
              that.onShow();
          });
        } else if (res.cancel) {
          Requ.showToast('已取消');
        }
      }
    })
  },

  lastgroup: function (e) {
    var optionList = JSON.stringify(this.data.optionList)
    new Request('updata_shopgroup.cls.php').request(
      () => {
        return {
          length: this.data.optionListlength,
          optionList: optionList,
        }
      },
      (res) => {
        wx.showToast({
          title: '提交成功',
          icon: 'success',
          duration: 1500,
          mask: true
        });
      this.onShow();

    });
    
  },
  
  
})


var types = ['default', 'primary', 'warn']
var pageObject = {
  data: {
    defaultSize: 'default',
    primarySize: 'default',
    warnSize: 'default',
    disabled: false,
    plain: false,
    loading: false
  },
  setDisabled: function (e) {
    this.setData({
      disabled: !this.data.disabled
    })
  },
  setPlain: function (e) {
    this.setData({
      plain: !this.data.plain
    })
  },
  setLoading: function (e) {
    this.setData({
      loading: !this.data.loading
    })
  },
  onGotUserInfo: function (e) {
    console.log(e.detail.errMsg)
    console.log(e.detail.userInfo)
    console.log(e.detail.rawData)
  },
}

for (var i = 0; i < types.length; ++i) {
  (function (type) {
    pageObject[type] = function (e) {
      var key = type + 'Size'
      var changedData = {}
      changedData[key] =
        this.data[key] === 'default' ? 'mini' : 'default'
      this.setData(changedData)
    }
  })(types[i])
}
